<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>短信内容修改</title>
<link href="../../layui/css/iconfont/iconfont.css" rel="stylesheet"
	type="text/css">
<link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
<link href="../../css/style.css" rel="stylesheet" type="text/css">
<script src="../../layui/layui.js"></script>
<script src="../../js/jquery-1.8.3.js"></script>
<script src="../../js/util.js"></script>
<script type="text/javascript">
        layui.use(['layer','laydate', 'form', 'jquery', 'element'], function () {
            var table = layui.table;
            var layer = layui.layer;
            var form = layui.form;
            var $ = layui.$;
            var laydate=layui.laydate;
            var mgrId=getUrlParam("mgrId");
            var cityId=getUrlParam("cityId");
            laydate.render({
                elem: '#effectTime'
                , type: 'datetime'
                , btns: ['clear', 'confirm']
            });
            //页面数据初始化
            $(document).ready(function () {
                var url = '/content/findContentByMgrId';
                var param = {
                    mgrId: mgrId
                };
                $.ajax({
                    type: 'post',
                    async: false,
                    url: url,
                    data: param,
                    beforeSend: function () {
                        loading = layer.load(0, {shade: false});
                    },
                    success: function (json) {
                        layer.close(loading);
                        var data;
                        if(json.code==0){
                            data = json.data;
                            if(!isNull(data.content)){
                            	 $("#oldContent").val(data.content);
                            }
                        }else {
                            layer.msg(json.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
            });

	    	 //查看短信条数
	    	$('#qryContentCount').on('click', function () {
            	$('#contentCount').show();

            	var param;
            	var content = $("#newContent").val();
            	var url;
    			var isThreshold = 0;
    			var threshold = 0;
    			var cycleThreshold = 0;
    			var cycle=0;
    			var url1="/template2/findTemplate";
    			var param1 = {
                    mgrId: mgrId
                };
                $.ajax({
                    type: 'post',
                    async: false,
                    url: url1,
                    data: param1,
                    beforeSend: function () {
                        loading = layer.load(0, {shade: false});
                    },
                    success: function (json) {
                        layer.close(loading);
                        var data;
                        if(json.code==0){
                            data = json.data;
                            isThreshold=data.isThreshold;
                            threshold=data.threshold;
                            cycleThreshold=data.cycleThreshold;
                            cycle=data.cycle;
                        }else {
                            layer.msg(json.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });

    			if(isNull(content)){
            		layer.msg("新短信内容为空！");
            		return false;
            	}else{
            		url="/util/contentCount";
            		param={
            			content : content
            		};

            		$.ajax({
            			type: 'post',
	                    data: param,
	                    url: url,
            			 beforeSend: function () {
	                        loading = layer.load(0, {shade: false});
	                    },
	                    success: function (json) {
	                        layer.close(loading);
	                        if (json.code == 0) {
	                            $("#contentNum").html(json.data);
	                            $("#contentLength").html(content.length);
	                             if(isThreshold==1){
	                            	$("#thresholdNum").html(" , 总计将发送："+(json.data)*threshold+"  条");
	                            }else if(isThreshold==2 &&cycle==1){
	                            	$("#thresholdNum").html(" , 每年将发送："+(json.data)*cycleThreshold+"  条");
	                            }else if(isThreshold==2 &&cycle==2){
	                            	$("#thresholdNum").html(" , 每月将发送："+(json.data)*cycleThreshold+"  条");
	                            }else if(isThreshold==2 &&cycle==3){
	                            	$("#thresholdNum").html(" , 每周将发送："+(json.data)*cycleThreshold+"  条");
	                            }else if(isThreshold==2 &&cycle==4){
	                            	$("#thresholdNum").html(" , 每天将发送："+(json.data)*cycleThreshold+"  条");
	                            }else if(isThreshold==3 &&cycle==1){
	                            	$("#thresholdNum").html(" , 总计将发送："+(json.data)*threshold+"  条 , 每年将发送："+(json.data)*cycleThreshold+"  条");
	                            }else if(isThreshold==3 &&cycle==2){
	                            	$("#thresholdNum").html(" , 总计将发送："+(json.data)*threshold+"  条 , 每月将发送："+(json.data)*cycleThreshold+"  条");
	                            }else if(isThreshold==3 &&cycle==3){
	                            	$("#thresholdNum").html(" , 总计将发送："+(json.data)*threshold+"  条 , 每周将发送："+(json.data)*cycleThreshold+"  条");
	                            }else if(isThreshold==3 &&cycle==4){
	                            	$("#thresholdNum").html(" , 总计将发送："+(json.data)*threshold+"  条 , 每天将发送："+(json.data)*cycleThreshold+"  条");
	                            }



	                        } else {
	                            layer.msg(json.msg);
	                        }
	                    }

            		});

            		$('#contentCount').show();
            	}


            });

            //表单提交
            form.on('submit(commit)', function (data) {
                var effectTime = $("#effectTime").val();  //时间范围内的开始时间
                var parm;
                var oldContent = $("#oldContent").val();
                var url ;
                if(isNull(oldContent)){
                	parm = {
	                    mgrId: mgrId,
	                    content:$("#newContent").val(),
	                    cityId:cityId
	                };
                	url = "/content/addContent";
                	$.ajax({
	                    type: 'post',
	                    data: parm,
	                    url: url,
	                    beforeSend: function () {
	                        loading = layer.load(0, {shade: false});
	                    },
	                    success: function (json) {
	                        layer.close(loading);
	                        if (json.code == 0) {
	                            layui.use('layer', function () {
	                                layer.msg('添加成功,点击确认返回模板配置管理界面', {
	                                    time: 0 //不自动关闭
	                                    , btn: ['确定']
	                                    , yes: function (index) {
	                                        layer.close(index);
	                                        location.href = "../templateManage/smsTemplateManage2.html";
	                                    }
	                                });
	                            });
	                        } else {
	                            layer.msg(json.msg);
	                        }
	                    },
	                    complete: function () {

	                    },
	                    error: function (data) {
	                        layer.msg('系统异常，请联系管理员处理!');
	                    }
	                });
                }else{
                	parm = {
	                    mgrId: mgrId,
	                    newContent:$("#newContent").val(),
	                    effectTime:effectTime
	                };
                	url = "/content/modifyContent";
                	$.ajax({
	                    type: 'post',
	                    data: parm,
	                    url: url,
	                    beforeSend: function () {
	                    	if(isNull(effectTime)){
	                    		layer.msg("短信生效时间不能为空");
		          				return false;
	                    	}
	                    	if(oldContent==$("#newContent").val()){
	                    		layer.msg("新短信内容与原短信内容相同!");
		          				return false;
	                    	}
	                        loading = layer.load(0, {shade: false});
	                    },
	                    success: function (json) {
	                        layer.close(loading);
	                        if (json.code == 0) {
	                        document.getElementById("commit").setAttribute("disabled", true);//设置不可点击
							document.getElementById("commit").style.backgroundColor  = '#C0C0C0';//设置背景色
	                            layui.use('layer', function () {
	                                layer.msg('修改成功,点击确认返回模板配置管理界面', {
	                                    time: 0 //不自动关闭
	                                    , btn: ['确定']
	                                    , yes: function (index) {
	                                        layer.close(index);
	                                        location.href = "../templateManage/smsTemplateManage2.html";
	                                    }
	                                });
	                            });
	                        } else {
	                            layer.msg(json.msg);
	                        }
	                    },
	                    complete: function () {

	                    },
	                    error: function (data) {
	                        layer.msg('系统异常，请联系管理员处理!');
	                    }
	                });
                }
                return false;
            });
        });
    </script>
</head>
<body>
	<!--nav-->
	<div class="layui-html-limit">

		<!--container-->
		<div class="layui-content-body">
			<div class="scroll-box">
				<div class="breadcrumb-box">
					<span class="layui-breadcrumb" lay-separator="|"> <a
						href="javascript:returnIndex2()">首页&nbsp;</a> <a
						href="../templateManage/smsTemplateManage2.html">
							&nbsp;短信项目模板配置管理&nbsp; </a> <a href=""> &nbsp;短信内容修改&nbsp; </a>
					</span>
				</div>
				<div class="content-box">
					<form class="layui-form layui-form-pane">
						<div class="layui-form-item layui-form-text">
							<label class="layui-form-label">原短信内容</label>
							<div class="layui-input-block">
								<textarea class="layui-textarea" disabled id="oldContent"></textarea>
							</div>
						</div>
						<div class="layui-form-item layui-form-text">
							<label class="layui-form-label">新短信内容</label>
							<div class="layui-input-block">
								<textarea placeholder="请输入内容" class="layui-textarea"
									lay-verify="required" id="newContent"></textarea>
							</div>
						</div>
						<div class="layui-form-item">
							<span>
								<button type="button" class="layui-btn layui-btn-normal"
									id="qryContentCount">查看短信条数</button>
							</span>
							<h4 class="tips-h4" id="contentCount"
								style="display:none;color:#F00">
								短信长度为&nbsp;&nbsp;<span id="contentLength">0</span>&nbsp;&nbsp;,&nbsp;将发送&nbsp;&nbsp;<span
									id="contentNum">0</span>&nbsp;&nbsp;条短信<span id="thresholdNum"></span>
							</h4>
						</div>
						<div class="layui-form-item">
							<div class="layui-inline">
								<label class="layui-form-label">生效日期</label>
								<div class="layui-input-inline" style="width: auto;">
									<input type="text" name="effectTime" id="effectTime"
										autocomplete="off" class="layui-input" placeholder="请选择">
								</div>
							</div>
						</div>
						<div class="layui-form-item">
							<button class="layui-btn layui-btn-normal" id="commit" lay-submit
								lay-filter="commit">提交</button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</body>
</html>